require(["config"],function(){
    require(["jquery","template","load"],function($,template){
        function listHandler(){
            this.init();
            this.isLoadingMore = false;
            this.page = 1; // 页码
            this.pageSize = 12; // 每页数据记录数
            this.onOff = true;
            this.chooseObj = {};
        };
        $.extend(listHandler.prototype,{
            // 页面初始化
            init(){
                // 页面数据加载
                this.listLoadData()
                // 页面事件处理
                this.listEventHandler();
            },
            listLoadData(){
                // 初始化页面加载数据
                this.loadListData();
                // 加载喜欢的商品
                this.loadLikeProduct();
            },
            // 事件处理函数
            listEventHandler(){
                // 筛选条件
                $(".list-submuen-item").on("click","a",$.proxy(this.screenHandler,this))
                // 筛选条件删除处理
                $(".screen-cond").on("click","a",$.proxy(this.deleteScreen,this));
                // 价格排序
                $(".last-submit").on("click",$.proxy(this.paoductPriceSort,this));
                // 滚动事件监听
                $(window).on("scroll",$.proxy(this.loadMoreData,this));
                // 价格事件处理
                $(".screen-pr").on("click",$.proxy(this.loadPrice,this));
                
            },
            // 页面内容数据加载
            loadListData(){
                $.getJSON("http://rap2api.taobao.org/app/mock/data/720657",data =>{
                    data = data.res_body.data;
                    const start = (this.page - 1) * this.pageSize;
                    const end = start + this.pageSize;
                    let listArray = data.slice(start, end);
                    const listHtml = template("listMain",{listItem:listArray});
                    $(".list-box").append(listHtml);
                    this.isLoadingMore = false;
                    if(this.page*this.pageSize >= data.length){ 
                        this.page = Math.ceil(data.length / 12);
                        return;
                    }
                })
            },
            // 滚动事件处理
            loadMoreData(){
                if(!this.isLoadingMore && this.checkScrollSlide()){
                    this.isLoadingMore = true;
                    this.page +=  1;
                    this.loadListData();
                    console.log("page: " , this.page);
                }
            },
            // 加载数据的条件判断
            checkScrollSlide(){
                const scrollTop = $('body,html').scrollTop();
                const height = $(window).height();
                const targetHeight = $(".cart-recommending").offset().top - 100;
                return targetHeight < scrollTop + height;
            },
            // 加载喜欢商品的数据
            loadLikeProduct(){
                $.getJSON("http://rap2api.taobao.org/app/mock/data/720098",data => {
                    let html = template("link_product",{linkProductArr:data.res_body.list});
                    $(".cart-recommending-list").html(html);
                })
            },
            // 筛选处理函数
            screenHandler(ev){
                let _this = ev.target,
                    _index = $(_this).parents(".list-submuen-item").index(),
                    _html = "";
                $(_this).addClass("active").siblings().removeClass("active");
                // 事件源的父元素的下标
                this.chooseObj[_index] = $(_this).text();
                for(let i = 0; i < $(".list-submuen-item").length; i++){
                    if(this.chooseObj[i]){
                        _html += `<mark>${this.chooseObj[i]}<a href="javascript:void(0);">x</a></mark>`;
                    }
                }
                $(".cond_con").html(_html);
            },
            // 删除筛选
            deleteScreen(ev){
                const _text = $(ev.target).parent("mark").text(); 
                const _len = _text.length;
                const _texts = $(ev.target).parent("mark").text().slice(0,_len - 1);
                for(let attr in this.chooseObj){
                    if(this.chooseObj[attr] == _texts){
                        delete this.chooseObj[attr];
                        const _as = $(".list-submuen-item-icons").eq(attr).find("a");
                        _as.first("a").addClass("active").siblings().removeClass("active");
                    }
                }
                $(ev.target).parent("mark").remove();
            },
            // list价格事件函数处理
            loadPrice(){
                $.getJSON("http://rap2api.taobao.org/app/mock/data/720657",data => {
                    const _data = data.res_body.data;
                    if(this.onOff){
                        _data.sort((a,b) =>{
                            return a.price - b.price
                        });
                        $(".screen-pr").addClass("screen-down").removeClass("screen-default screen-up");
                        this.onOff = false;
                    }else{
                        _data.sort((a,b) =>{
                            return b.price - a.price;
                        });
                        $(".screen-pr").addClass("screen-up").removeClass("screen-down screen-default");
                        this.onOff = true;
                    }
                    const  _listHtml = template("listMain",{listItem:_data});
                    $(".list-box").html(_listHtml);
                })
            },
            // 商品价格排序处理函数
            paoductPriceSort(){
                let minPrice = $(".min_price").val(),
                    maxPrice = $(".max_price").val(),
                    reg = new RegExp("^[0-9]*$");
                    if(minPrice && maxPrice){
                        if(reg.test(minPrice) && reg.test(maxPrice)){
                            if(minPrice > maxPrice){
                                alert("最低价不能高于最高价")
                            }else{
                                $.getJSON("http://rap2api.taobao.org/app/mock/data/720657",data =>{
                                    let listArray = data.res_body.data;
                                    const newListArr = listArray.filter((curr) => {
                                        return curr.price > minPrice && curr.price < maxPrice
                                    })
                                    const sortArr = newListArr.sort((a,b) => {
                                        return a.price - b.price;
                                    })
                                    const  _listHtml = template("listMain",{listItem:sortArr});
                                    $(".list-box").html(_listHtml);
                                })
                            }
                        }else{
                            alert("请输入数字");
                            $(".min_price").val("");
                            $(".max_price").val("");
                        }
                    }else{
                        alert("请输入最低价格和最高价格");
                       
                    }
            }
        });
        new listHandler();
    })
})






// const list = [
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
//     {id, title, price},
// ]

// const newArray = list.filter(curr=>{
//     return curr.price >= ? && curr.price <= ?
// });

// newArray.sort((a, b)=>{
//     return a.price - b.price;
// });